<script setup>
import HelloWorld from '../components/HelloWorld.vue'
import PreView from '../components/PreView.vue'
import Editor from '../components/Editor.vue'
import { ref, nextTick } from 'vue'
import mdkatex from 'katex';

const latexInput = ref('')
const renderResult = ref('')
const markdownInput = ref('')

const renderLatex = () => {
  renderResult.value = latexInput.value
}

const clearInput = () => {
  latexInput.value = ''
  renderResult.value = ''
}

const copyLatex = () => {
  navigator.clipboard.writeText(latexInput.value)
}

const clearMarkdown = () => {
  markdownInput.value = ''
  markdownResult.value = ''
}

const copyMarkdown = () => {
  navigator.clipboard.writeText(markdownInput.value)
}
</script>

<template>
  <div class="math-formula">
    <katex-element expression="E = mc^2" />
    <katex-element expression="\sum_{i=1}^n i = \frac{n(n+1)}{2}" />
    <katex-element expression="R^{\rho}_{\sigma\mu\nu} + R^{\rho}_{\mu\nu\sigma} + R^{\rho}_{\nu\sigma\mu} = 0" />
    <katex-element expression="R^{\rho}_{\sigma\mu\nu} = \partial_\mu\Gamma^{\rho}_{\nu\sigma} - \partial_\nu\Gamma^{\rho}_{\mu\sigma} + \Gamma^{\rho}_{\mu\lambda}\Gamma^{\lambda}_{\nu\sigma} - \Gamma^{\rho}_{\nu\lambda}\Gamma^{\lambda}_{\mu\sigma}" />
    <katex-element expression="\frac{\partial}{\partial x} \left( \frac{\partial}{\partial y} f(x, y) \right) = \frac{\partial^2 f}{\partial x \partial y}" />
    <katex-element expression="\begin{aligned}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{aligned}" />
  </div>

  <div class="container">
    <div class="section latex-section">
      <h2>LaTeX编辑器</h2>
      <div class="input-group">
        <input
          v-model="latexInput"
          placeholder="输入LaTeX公式..."
          class="latex-input"
        />
        <div class="button-group">
          <button @click="renderLatex" class="action-button render">渲染</button>
          <button @click="clearInput" class="action-button clear">清除</button>
          <button @click="copyLatex" class="action-button copy">复制</button>
        </div>
      </div>
      <div v-if="renderResult" class="render-result">
        <katex-element :expression="renderResult" />
      </div>
    </div>

    <div class="section markdown-section">
      <h2>Markdown编辑器</h2>
      <div class="input-group">
        <Editor
          v-model="markdownInput"
          class="markdown-input"
        />
        <div class="button-group">
          <button @click="renderMarkdown" class="action-button render">渲染</button>
          <button @click="clearMarkdown" class="action-button clear">清除</button>
          <button @click="copyMarkdown" class="action-button copy">复制</button>
        </div>
      </div>
      <PreView :markdown="markdownInput"></PreView>
    </div>
  </div>
</template>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  gap: 2rem;
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
}

.section {
  background-color: #f5f5f5;
  border-radius: 8px;
  padding: 1.5rem;
}

.section h2 {
  margin-top: 0;
  margin-bottom: 1rem;
  color: #2c3e50;
}

.markdown-input {
  width: 100%;
  min-height: 200px;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  resize: vertical;
}

.math-formula {
  margin: 20px;
  font-size: 1.2em;
  display: flex;
  flex-direction: column;
  gap: 1em;
  align-items: center;
}
</style>

<style>
.latex-input-container {
  margin: 20px;
  padding: 20px;
  border-radius: 8px;
  background-color: #f5f5f5;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.latex-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.button-group {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.action-button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
  transition: background-color 0.3s;
}

.action-button.render {
  background-color: #4CAF50;
  color: white;
}

.action-button.clear {
  background-color: #f44336;
  color: white;
}

.action-button.copy {
  background-color: #2196F3;
  color: white;
}

.action-button:hover {
  opacity: 0.9;
}
</style>